<?php

namespace Admin\Controller;

use Think\Controller;

class SectionController extends BaseController {
    /*
     * 科室列表
     */

    public function section_list() {
        $res = M("section_type")->select();
        $this->assign("data", $res);
        $this->display();
    }

    /**
     * 添加科室
     */
    public function section_add() {
        if (IS_POST) {
            $name = I("name");
            $data["name"] = $name;
            $db = M("section_type");
            $re = $db->where($data)->find();
            if ($re) {
                $this->error("科室已存在");
                exit;
            } else {
                $res = $db->add($data);
                if ($res) {
                    $this->success("添加成功", U("section_list"));
                    exit;
                } else {
                    $this->error("添加失败");
                    exit;
                }
            }
        }
        $this->display();
    }

    /**
     * 科室编辑
     */
    public function section_edit() {
        if (IS_POST) {
            $id = I("id");
            $name = I("name");
            if (empty($id) && empty($name)) {
                $this->error("参数不完整");
                exit;
            } else {
                $data["name"] = $name;
                $db = M("section_type");
                $re = $db->where($data)->find();
                if ($re) {
                    $this->error("科室已存在");
                    exit;
                } else {
                    $where["id"] = $id;
                    $res = $db->where($where)->save($data);
                    if ($res) {
                        $this->success("修改成功", U("section_list"));
                        exit;
                    } else {
                        $this->error("科室未做修改");
                        exit;
                    }
                }
            }
        }
        $id = I("id");
        $re = M("section_type")->where("id=" . $id)->find();
        $this->assign("data", $re);
        $this->display("section_add");
    }

    /**
     * 科室删除
     */
    public function section_del() {
        $id = I("id");
        $re = M("section_type")->where("id=".$id)->delete();
        if($re){
            $this->success("删除成功",U("section/section_list"));
        }else{
            $this->error("删除失败");
        }
        
    }

}
